﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.Membership
{
    public static class Index
    {
        public static Adams.Admin.DataTransfer.Membership.Index.PageLoad PageLoad()
        {
            var data = new Adams.Admin.DataTransfer.Membership.Index.PageLoad();
            
            return data;
        }

        public static Adams.Admin.DataTransfer.Membership.Index.GridLoad GridLoad(DataTransfer.GridLoadInput gridinput)
        {
            var data = new Adams.Admin.DataTransfer.Membership.Index.GridLoad();
            data.Records = new Adams.Shared.Entities.Membership.MemberCollection();

            string filter = string.IsNullOrWhiteSpace(gridinput.Search) ? string.Empty : "FREETEXT ((FirstName,LastName,Email), '" + gridinput.Search.Replace("'", "''") + "' )";

            string dbq = DB.BuildPagingQuery(gridinput.StartRow, gridinput.PageSize,
                "*",
                "Membership.Members",
                filter, string.Empty, gridinput.Sort + " " + gridinput.SortDirection, "MemberID");

            using (SqlDataReader dr = DB.RunDataReader(dbq, CommandType.Text))
            {
                dr.Read();
                data.TotalRecords = dr[0].DbInt();

                dr.NextResult();
                while (dr.Read())
                {
                    var Member = new Adams.Shared.Entities.Membership.Member();
                    Member.ID = dr["MemberID"].DbInt();
                    Member.FirstName = dr["FirstName"].DbString();
                    Member.LastName = dr["LastName"].DbString();
                    Member.Email = dr["Email"].DbString();
                    Member.IsEmailConfirmed = dr["IsEmailConfirmed"].DbBool();
                    Member.IsActive = dr["IsActive"].DbBool();
                    Member.IsLocked = dr["IsLocked"].DbBool();
                    data.Records.Add(Member);
                }
            }

            return data;
        }
    }
}
